package com.chris.homecare.screens
{
	import com.chris.homecare.components.BreadcrumbComponenet;
	import com.chris.homecare.components.HeaderComponent;
	import com.chris.homecare.components.TextAreaComponent;
	import com.chris.homecare.data.FetchRemoteData;
	import com.chris.homecare.utils.CommonUtils;
	import com.chris.homecare.utils.ShowNativeAlert;
	
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.net.URLRequestMethod;
	
	import feathers.controls.Button;
	import feathers.controls.Label;
	import feathers.controls.LayoutGroup;
	import feathers.controls.Screen;
	import feathers.controls.ScrollContainer;
	import feathers.controls.TextInput;
	import feathers.events.FeathersEventType;
	import feathers.layout.HorizontalLayout;
	import feathers.layout.VerticalLayout;
	
	import pl.mateuszmackowiak.nativeANE.events.NativeDialogEvent;
	
	import starling.display.Quad;
	import starling.events.Event;
	
	public class RequestScreen extends Screen
	{
		private var contentLayoutGroup:LayoutGroup;
		private var headerComp:HeaderComponent;
		private var breadcrumbComp:BreadcrumbComponenet;
		private var formGroup:ScrollContainer;

		private var gapingQuad1:Quad;

		private var nameTextInput:TextInput;
		private var emailTextInput:TextInput;
		private var contNumTextInput:TextInput;
		private var messageTxtComp:TextAreaComponent;

		private var submitBtnLayoutGroup:LayoutGroup;
		private var requestForTxtInput:TextInput;
		
		private var isKeyBoardActive:Boolean = false;

		private var messageLabel:Label;
		private var requstForLabel:Label;
		private var nameLabel:Label;
		private var emailLabel:Label;
		private var contactNumLabel:Label;
		
		public function RequestScreen()
		{
			super();
		}
		
		override protected function initialize():void
		{
			contentLayoutGroup = new LayoutGroup();
			var contentLayout:VerticalLayout = new VerticalLayout();
			contentLayout.horizontalAlign = VerticalLayout.HORIZONTAL_ALIGN_CENTER;
			contentLayoutGroup.layout = contentLayout;
			addChild(contentLayoutGroup);
			
			headerComp = new HeaderComponent();
			headerComp.addEventListener(starling.events.Event.COMPLETE, onMenuBtnTrigger);
			contentLayoutGroup.addChild(headerComp);
			
			breadcrumbComp = new BreadcrumbComponenet("Request Information");
			contentLayoutGroup.addChild(breadcrumbComp);
			
			gapingQuad1 = new Quad(10,10*CommonUtils.scaleValue,0xFFFFFF);
			contentLayoutGroup.addChild(gapingQuad1);
			
			formGroup = new ScrollContainer();
			contentLayoutGroup.addChild(formGroup);
			
			var formLayout:VerticalLayout = new VerticalLayout();
			formLayout.gap = 5*CommonUtils.scaleValue;
			formGroup.layout = formLayout;
			
			nameLabel = new Label();
			nameLabel.nameList.add("form-heading-lbl");
			nameLabel.text = "Name:";
			formGroup.addChild(nameLabel);
			
			nameTextInput = new TextInput();
			nameTextInput.nameList.add("custom-text-input");
			//nameTextInput.addEventListener( FeathersEventType.FOCUS_IN, onNameTextInputFocus);
			nameTextInput.prompt = "Please enter your name";
			formGroup.addChild(nameTextInput);
			
			var gapingQuad2:Quad = new Quad(10,10*CommonUtils.scaleValue,0xFFFFFF);
			formGroup.addChild(gapingQuad2);
			
			emailLabel = new Label();
			emailLabel.text = "Email:";
			emailLabel.nameList.add("form-heading-lbl");
			formGroup.addChild(emailLabel);
			
			emailTextInput = new TextInput();
			emailTextInput.nameList.add("custom-text-input");
			//emailTextInput.addEventListener( FeathersEventType.FOCUS_IN, onEmailTextInputFocus);
			emailTextInput.prompt = "Please enter your email";
			emailTextInput.x = 10;
			formGroup.addChild(emailTextInput);
			
			var gapingQuad3:Quad = new Quad(10,10*CommonUtils.scaleValue,0xFFFFFF);
			formGroup.addChild(gapingQuad3);
			
			contactNumLabel = new Label();
			contactNumLabel.text = "Contact Number:";
			contactNumLabel.nameList.add("form-heading-lbl");
			formGroup.addChild(contactNumLabel);
			
			contNumTextInput = new TextInput();
			contNumTextInput.nameList.add("custom-text-input");
			contNumTextInput.prompt = "Please enter your contact number";
			//contNumTextInput.addEventListener( FeathersEventType.FOCUS_IN, onContactTextInputFocus);
			contNumTextInput.restrict = "0-9";
			contNumTextInput.x = 10;
			formGroup.addChild(contNumTextInput);
			
			var gapingQuad4:Quad = new Quad(10,10*CommonUtils.scaleValue,0xFFFFFF);
			formGroup.addChild(gapingQuad4);
			
			requstForLabel = new Label();
			requstForLabel.text = "Request For:";
			requstForLabel.nameList.add("form-heading-lbl");
			formGroup.addChild(requstForLabel);
			
			requestForTxtInput = new TextInput();
			requestForTxtInput.nameList.add("custom-text-input");
			requestForTxtInput.prompt = "Please tell us about your request";
			//requestForTxtInput.addEventListener(FeathersEventType.FOCUS_IN, onRequestTextInputFocus);
			requestForTxtInput.x = 10;
			formGroup.addChild(requestForTxtInput);
			
			var gapingQuad5:Quad = new Quad(10,10*CommonUtils.scaleValue,0xFFFFFF);
			formGroup.addChild(gapingQuad5);
			
			messageLabel = new Label();
			messageLabel.text = "Message:";
			messageLabel.nameList.add("form-heading-lbl");
			formGroup.addChild(messageLabel);
			
			messageTxtComp = new TextAreaComponent();
			messageTxtComp.x = 10;
			messageTxtComp.height = 200;
			formGroup.addChild(messageTxtComp);
			messageTxtComp.textArea.addEventListener(FeathersEventType.FOCUS_IN, onMessageAreaFocus);

			var gapingQuad6:Quad = new Quad(10,20,0xFFFFFF);
			formGroup.addChild(gapingQuad6);
			
			submitBtnLayoutGroup = new LayoutGroup();
			var submitBtnLayout:HorizontalLayout = new HorizontalLayout();
			submitBtnLayout.horizontalAlign = HorizontalLayout.HORIZONTAL_ALIGN_CENTER;
			submitBtnLayout.paddingBottom = 10*CommonUtils.scaleValue;
			submitBtnLayoutGroup.layout = submitBtnLayout;
			formGroup.addChild(submitBtnLayoutGroup);
			
			var submitBtn:Button = new Button();
			submitBtn.label = "Submit";
			submitBtn.addEventListener(starling.events.Event.TRIGGERED, onFormSubmit);
			submitBtn.nameList.add("performance-btn");
			submitBtnLayoutGroup.addChild(submitBtn);
			
			super.initialize();
		}
		
		override protected function draw():void
		{
			if(!isKeyBoardActive)
			{
				formGroup.height = stage.stageHeight - (gapingQuad1.y + 20*CommonUtils.scaleValue);
			}
			else 
			{
				formGroup.height =  (formGroup.height - messageLabel.y);
				formGroup.scrollToPosition(0,messageLabel.y, 0);
			}
			
			submitBtnLayoutGroup.width = stage.stageWidth-20;
			requestForTxtInput.width = messageTxtComp.width = contNumTextInput.width = emailTextInput.width = nameTextInput.width = stage.stageWidth - 20;
			super.draw();
		}
		
		
		private function onMenuBtnTrigger(event:starling.events.Event):void
		{
			this.dispatchEventWith(starling.events.Event.COMPLETE);
		}
		
		
		private function onFormSubmit():void
		{
			var alertObject:Object;
			
			if(nameTextInput.text != "" && CommonUtils.isValidUserEmail(emailTextInput.text) && 
				CommonUtils.isValidPhoneNumber(contNumTextInput.text) && requestForTxtInput.text != "")
			{
				trace("All Fields are valid....");
				submitRequest();
				return;
			}
			
			else if(nameTextInput.text == "")
			{
				nameTextInput.setFocus();
				alertObject = new Object();
				alertObject.message = "Name Cannot be empty!";
				alertObject.title = "Speiuss";
				alertObject.buttons = Vector.<String>(["OK"]);
				ShowNativeAlert.showAlert(alertObject);
			}
			
			else if(!CommonUtils.isValidUserEmail(emailTextInput.text))
			{
				emailTextInput.setFocus();
				emailTextInput.setFocus();
				alertObject = new Object();
				alertObject.message = "Please give valid email adress!";
				alertObject.title = "Speiuss";
				alertObject.buttons = Vector.<String>(["OK"]);
				ShowNativeAlert.showAlert(alertObject);
			}
			
			else if(!CommonUtils.isValidPhoneNumber(contNumTextInput.text))
			{
				contNumTextInput.setFocus();
				alertObject = new Object();
				alertObject.message = "Please give valid phone number!";
				alertObject.title = "Speiuss";
				alertObject.buttons = Vector.<String>(["OK"]);
				ShowNativeAlert.showAlert(alertObject);
			}
			
			else if(requestForTxtInput.text == "")
			{
				requestForTxtInput.setFocus();
				alertObject = new Object();
				alertObject.message = "Please tell us about your request!";
				alertObject.title = "Speiuss";
				alertObject.buttons = Vector.<String>(["OK"]);
				ShowNativeAlert.showAlert(alertObject);
			}
		}
		
		private function onContactTextInputFocus():void
		{
			formGroup.scrollToPosition(0,contactNumLabel.y,0);
		}
		
		private function onEmailTextInputFocus():void
		{
			formGroup.scrollToPosition(0,emailLabel.y,0);
		}
		
		private function onNameTextInputFocus():void
		{
			formGroup.scrollToPosition(0,nameLabel.y,0);
		}
		
		private function onRequestTextInputFocus():void
		{
			formGroup.scrollToPosition(0,requstForLabel.y,0);
		}
		
		private function onMessageAreaFocus():void
		{
			this.addEventListener(FeathersEventType.SOFT_KEYBOARD_DEACTIVATE, onSoftKeyBoardDeactivate);
			isKeyBoardActive = true;
			this.invalidate(INVALIDATION_FLAG_SIZE);
		}
		
		private function submitRequest():void
		{
			var url:String = "http://speiusscare.co.uk/sitterApp/api.php?mod=users&method=clientRequestToAdmin&mem_name=" +nameTextInput.text+ 
				"&mem_phone=" +contNumTextInput.text+ "&mem_address=" +messageTxtComp.textArea.text+ "&mem_enquiry=" +requestForTxtInput.text+ "&mem_mail=" +emailTextInput.text;
			var request:URLRequest=new URLRequest();
			request.url = url;
			request.method = URLRequestMethod.GET;
			
			var loader:URLLoader = new URLLoader();
			loader.addEventListener(flash.events.Event.COMPLETE, onRequestSuccessfullPosted);
			loader.addEventListener(IOErrorEvent.IO_ERROR, onError);
			loader.load(request);
		}
		
		protected function onRequestSuccessfullPosted(event:flash.events.Event):void
		{
			var alertObject:Object = new Object();
			alertObject.message = "Your Request was successfully Posted...";
			alertObject.title = "Speiuss";
			alertObject.buttons = Vector.<String>(["OK"]);
			ShowNativeAlert.showAlertAndGetResponse(alertObject, okButtonTriggeredHandler);
		}
		
		protected function okButtonTriggeredHandler(event:NativeDialogEvent):void
		{
			FetchRemoteData.choosenJobData = null;
			this.dispatchEventWith(starling.events.Event.COMPLETE);
		}
		
		protected function onError(event:IOErrorEvent):void
		{
			var alertObject:Object = new Object();
			alertObject.message = "Cannot able process your request. \nPlease check your internet connection!";
			alertObject.title = "Speiuss";
			alertObject.buttons = Vector.<String>(["OK"]);
			ShowNativeAlert.showAlert(alertObject);
		}
		
		private function onSoftKeyBoardDeactivate():void
		{
			this.removeEventListener(FeathersEventType.SOFT_KEYBOARD_DEACTIVATE, onSoftKeyBoardDeactivate);
			isKeyBoardActive = false;
			this.invalidate(INVALIDATION_FLAG_SIZE);
			nameTextInput.setFocus();
		}
	}
}